import DateUtil from "../engine/DateUtil";
import { Logger } from "../engine/Logger";
import { UIHelper } from "../game/util/UIHelper";
import { NativeSdkHelper } from "./NativeSdkHelper";
import { VoiceCodeHelper } from "./VoiceCodeHelper";


const Native_Android_Recorder_Class: string = "com/jhaosdk/voicesdk/VoiceRecorder"
const Native_Android_Play_Class: string = "com/jhaosdk/voicesdk/VoicePlayer"
const Native_IOS_Class: string = "VoiceSDK"

export class VoiceSdkHelper {


    private static _voiceMediaPath: string
    private static startVoiceTime: number = 0
    private static timeCount;
    public static init() {
        VoiceCodeHelper.init()
        // Logger.log("VoiceSdkHelper.init11==", cc.sys.isNative)
        if (cc.sys.isNative) {
            window["VoiceSdkHelper"] = VoiceSdkHelper;
            this._voiceMediaPath = jsb.fileUtils.getWritablePath() + "voicemsgs/";
            if (!jsb.fileUtils.isDirectoryExist(this._voiceMediaPath)) {
                jsb.fileUtils.createDirectory(this._voiceMediaPath);
            }
            Logger.log("VoiceSdkHelper.init=", this._voiceMediaPath)
            this.setStorageDir()
        }
    }
    private static setStorageDir() {
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Recorder_Class, "setStorageDir", "(Ljava/lang/String;)V", this._voiceMediaPath);
        }
        else if (NativeSdkHelper.isIos()) {
            Logger.log("VoiceSdkHelper.setStorageDir=", this._voiceMediaPath)
            jsb.reflection.callStaticMethod(Native_IOS_Class, "setStorageDir:", this._voiceMediaPath);
        }
    }

    public static prepare(fileName: string = "record.amr") {
        if (!cc.sys.isNative) {
            return;
        }
        cc.audioEngine.pauseAll()
        this.clearCache(fileName);
        this.startVoiceTime = DateUtil.now()
        this.timeCount = setTimeout(() => {
            this.finishRecord()
        }, 5000)
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Recorder_Class, "prepare", "(Ljava/lang/String;)V", fileName);
        }
        else if (NativeSdkHelper.isIos()) {
            jsb.reflection.callStaticMethod(Native_IOS_Class, "prepareRecord:", fileName);
        }
    }

    private static clearCache(filename: string) {
        if (cc.sys.isNative) {
            let url: string = this._voiceMediaPath + filename;
            if (jsb.fileUtils.isFileExist(url)) {
                jsb.fileUtils.removeFile(url);
            }
            if (jsb.fileUtils.isFileExist(url + ".wav")) {
                jsb.fileUtils.removeFile(url + ".wav");
            }
        }
    }

    public static cancel() {
        if (!cc.sys.isNative) {
            return;
        }
        clearTimeout(this.timeCount)
        cc.audioEngine.resumeAll()
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Recorder_Class, "cancel", "()V");
        }
        else if (NativeSdkHelper.isIos()) {
            jsb.reflection.callStaticMethod(Native_IOS_Class, "cancelRecord:", "");
        }
    }

    public static finishRecord() {
        if (!cc.sys.isNative) {
            return;
        }
        clearTimeout(this.timeCount)
        cc.audioEngine.resumeAll()
        let passTime: number = DateUtil.now() - this.startVoiceTime;
        if (passTime < 1000) {
            UIHelper.showTip("语音时间小于1秒，不能发送")
            this.cancel()
            return;
        }
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Recorder_Class, "release", "()V");
        }
        else if (NativeSdkHelper.isIos()) {
            jsb.reflection.callStaticMethod(Native_IOS_Class, "finishRecord:", "");
        }
        setTimeout(() => {
            // this.play()
        }, 1000);
    }

    public static getVoiceLevel(maxLevel: number) {
        if (NativeSdkHelper.isAndroid()) {
            return jsb.reflection.callStaticMethod(Native_Android_Recorder_Class, "getVoiceLevel", "(I)I", maxLevel);
        }
        else if (NativeSdkHelper.isIos()) {
            return Math.floor(Math.random() * maxLevel + 1);
        }
        else {
            return Math.floor(Math.random() * maxLevel + 1);
        }
    }

    public static getVoiceData(filename: string = "record.amr") {
        if (cc.sys.isNative) {
            var url = this._voiceMediaPath + filename;
            console.log("getVoiceData:" + url);
            let fileData = jsb.fileUtils.getDataFromFile(url);
            if (fileData) {
                var content = VoiceCodeHelper.encode(fileData);
                return content;
            }
        }
        return "";
    }

    public static play(filename: string = "voicemsg.amr") {
        //return;
        if (!cc.sys.isNative) {
            return;
        }
        cc.audioEngine.pauseAll()
        if (NativeSdkHelper.isAndroid()) {
            jsb.reflection.callStaticMethod(Native_Android_Play_Class, "play", "(Ljava/lang/String;)V", filename);
        }
        else if (NativeSdkHelper.isIos()) {
            jsb.reflection.callStaticMethod(Native_IOS_Class, "play:", filename);
        }
    }


    public static writeVoice(voiceData: string, filename: string = "voicemsg.amr") {
        if (!cc.sys.isNative) {
            return;
        }
        if (voiceData && voiceData.length > 0) {
            let fileData = VoiceCodeHelper.decode(voiceData);
            let url = this._voiceMediaPath + filename;
            this.clearCache(filename);
            Logger.log("writeVoice===============", url)
            jsb.fileUtils.writeDataToFile(fileData, url);
        }
    },
}